package com.woniuxy.woniuhis.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.woniuhis.entity.RegisInfo;
import com.woniuxy.woniuhis.exception.RegisInfoServiceException;
import com.woniuxy.woniuhis.mapper.PatientMapper;
import com.woniuxy.woniuhis.mapper.RegisInfoMapper;
import com.woniuxy.woniuhis.service.RegisInfoService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Service
public class RegisInfoServiceImpl implements RegisInfoService{
	
	@Autowired
	RegisInfoMapper regisInfoMapper;
	@Autowired
	PatientMapper patientMapper;

	@Override
	public void addRegisInfo(RegisInfo regisInfo) {
		try {
			regisInfo.setRegisInfoId(UUIDUtil.uuid32());
			regisInfo.setRegisDate(new Date());
			regisInfo.setPayStatus(false);
			regisInfoMapper.insertRegisInfo(regisInfo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegisInfoServiceException("系统维护中");
		}
	}

	/**
	 * 查询当日挂号信息
	 */
	@Override
	public ResponseJson<RegisInfo> todayRegisInfo() {
		try {
			//查询当日所有挂号信息
			RegisInfo regisInfo = new RegisInfo(null, null, null, null, null, new Date(), null);
			List<RegisInfo> regisInfos = regisInfoMapper.selectRegisInfoByCondition(regisInfo);
			//创建layui能识别的数据结构
			ResponseJson<RegisInfo> responseJson = new ResponseJson<RegisInfo>();
			responseJson.setCode(0);
			responseJson.setCount(regisInfos.size());
			responseJson.setData(regisInfos);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegisInfoServiceException("系统维护中");
		}
	}

	@Override
	public Integer regisCount(int i) {
		try {
			Integer count = regisInfoMapper.regisInfoCountByDay(i);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegisInfoServiceException("系统维护中");
		}
	}

	@Override
	public Integer notPayStatus() {
		try {
			RegisInfo regisInfo = new RegisInfo(null, null, null, null, null, null, false);
			List<RegisInfo> list = regisInfoMapper.selectRegisInfoByCondition(regisInfo );
			return list.size();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegisInfoServiceException("系统维护中");
		}
	}

	@Override
	public Integer[] regisOfficeCount(int i) {
		try {
			Integer[] arr = new Integer[8];
			//将日期转化为请求需要的日期
			Date date = new Date();
			Calendar calendar = Calendar.getInstance();
			int today = calendar.get(Calendar.DATE);
			calendar.set(Calendar.DATE, today-i);
			date = calendar.getTime();
			//创建一个查询的实体对象
			RegisInfo regisInfo = new RegisInfo(null, null, null, null, null, date, null);
			//遍历每一个科室，第i日挂号数量
			for(int n=0;n<8;n++) {
				regisInfo.setOfficeId(n+1+"");
				List<RegisInfo> list = regisInfoMapper.selectRegisInfoByCondition(regisInfo);
				arr[n] = list.size();
			}
			return arr;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegisInfoServiceException("系统维护中");
		}
	}

}
